package com.fone.player.sns;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.fone.player.bean.RecordData;
import com.fone.player.client.Callback;
import com.fone.player.client.CollectionlsRst;
import com.fone.player.client.CollectionlsRst.Cnt;
import com.fone.player.client.Error;
import com.fone.player.client.LoginspaceRst;
import com.fone.player.client.LoginspaceRst.Bind;
import com.fone.player.client.LoginspaceRst.Binds;
import com.fone.player.client.Request;
import com.fone.player.client.RstSerializer;
import com.fone.player.constant.FoneConstant;
import com.fone.player.entity.PlayRecord;
import com.fone.player.sns.bean.SNSUserInfo;
import com.fone.player.sns.impl.SNSLoginManager;
import com.fone.player.storage.SharedPreferenceModule;
import com.fone.player.storage.StorageModule;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;
import com.fone.player.util.SerializationUtil;

public class UserInfoManager {

	private static String TAG = "UserInfoManager";

	public static final String HISTORICALUSERLIST = "historical_UserList.txt";

	public static final int NEW_USER = 1;
	public static final int UPDATE_USERINFO = 2;

	private static Context mContext;

	public static final String USER = "user.txt";

	public static final String USER_SEQID = "seq_id"; // seqid
	public static final String USER_TYPE = "user_type"; // 用户类型
	public static final String USER_MODIFY_ACCOUNT = "user_modify_account";// 帐号是否可以修改（付费帐户可以修改一次）
	public static final String USER_FEEURL = "user_feeurl";// 引导普通用户计费（isflag=0时有效）
	public static final String USER_DISPLAY_ID = "user_display_id";// 用户displayid
	public static final String USER_ID = "user_id";// 用户登录帐号
	public static final String USER_NICKNAME = "user_nickname";// 昵称
	public static final String USER_STATE = "user_state";// 用户状态: 2：club
															// 用户；0：停用；1：启用
															// 一个有效的用户默认是1 ??
	public static final String USER_VIP_STATE = "user_vip_type";// 0:非Vip；1：vip；3：运营帐号
	public static final String USER_VIP_EXPIRESTIME = "vip_expires_time";// Vip
																			// 过期时间
	public static final String USER_HEAd_PIC = "user_head_pic";// 头像地址
	public static final String USER_GENDER = "user_gender";// 用户性别
	public static final String USER_VB_COUNT = "user_vb_count";// 用户vb数量
	public static final String USER_LOGIN_STATE = "user_login_state";// 用户登录状态

	/**
	 * 初始化帐户管理类 需在启动应用时做初始化
	 * 
	 * @param context
	 */
	public static void initUserInfoManager(Context context) {
		mContext = context;
	}

	/**
	 * 登录成功后保存服务器返回的用户信息
	 * 
	 * @param mContext
	 * @param userInfo
	 *            用户信息
	 * @param saveType
	 *            保存类型 NEW_USER 全新用户 UPDATE_USERINFO 更新用户信息
	 */
	public static synchronized void saveLoginUserInfo(LoginspaceRst userInfo,
			int saveType) {
		if (userInfo != null) {
			switch (saveType) {
			case NEW_USER:
				saveNewUser(userInfo);
				break;
			case UPDATE_USERINFO:
				updateUserInfo(userInfo);
				break;
			default:
				break;
			}
			updateCipher(userInfo.seqid);
		}
		doPlayRecordSynchronize();
	}

	/**
	 * 
	 * 同步播放记录到服务器
	 * 
	 * @return void
	 * @throws
	 */
	private static void doPlayRecordSynchronize() {
		L.v(TAG, "doPlayRecordSynchronize", "doPlayRecordSynchronize start !!!");
		ArrayList<PlayRecord> recordList = StorageModule.getInstance()
				.getPlayRecordList(PlayRecord.RECORD_PLAYER_NETWORK);
		List<RecordData> records = null;
		if (recordList != null) {
			records = new ArrayList<RecordData>();
			for (PlayRecord record : recordList) {
				if (record.getPlayRecordSynchronzieType() == 1
						|| record.getPlayRecordSynchronzieType() == 2) {
					RecordData data = new RecordData();
					data.ccid = record.getPlayRecordCCID();
					data.cid = record.getPlayRecordCID();
					data.clid = record.getPlayRecordCLID();
					data.videoid = record.getPlayRecordVideoID();
					data.type = record.getPlayRecordSynchronzieType();
					data.secs = record.getPlayRecordTotalTime();
					data.tp = record.getPlayRecordAlreadyPlayTime();
					data.updatetime = record.getPlayRecordCreateTime();
					records.add(data);
				}
			}
		}
		L.v(TAG, "doPlayRecordSynchronize",
				"doPlayRecordSynchronize request sever start !!!");
		QuickPlayCallBack callBack = new QuickPlayCallBack();
		// 向服务器同步播放记录，并获取播放记录
		if (records != null && records.size() > 0) {
			Request.getInstance().collectionls(0, 2, records, callBack);
		}
		L.v(TAG, "doPlayRecordSynchronize", "doPlayRecordSynchronize end !!!");
	}

	/**
	 * 更新已登录用户的用户信息
	 * 
	 * @param userInfo
	 */
	private static void updateUserInfo(LoginspaceRst userInfo) {

		LoginspaceRst info = readserializedUserInfo();

		if (info == null) {
			saveNewUser(userInfo);
			return;
		}

		if (!TextUtils.isEmpty(userInfo.type)) {
			SharedPreferenceModule.getInstance().setString(USER_TYPE,
					userInfo.type);
			info.type = userInfo.type;
		}
		if (!TextUtils.isEmpty(userInfo.viptype)) {
			SharedPreferenceModule.getInstance().setString(USER_VIP_STATE,
					userInfo.viptype);
			info.viptype = userInfo.viptype;
		}
		if (!TextUtils.isEmpty(userInfo.isflag)) {
			if ("0".equals(userInfo.isflag)) {
				SharedPreferenceModule.getInstance().setBoolean(
						USER_MODIFY_ACCOUNT, false);
			} else {
				SharedPreferenceModule.getInstance().setBoolean(
						USER_MODIFY_ACCOUNT, true);
			}
			info.isflag = userInfo.isflag;
		}
		if (!TextUtils.isEmpty(userInfo.seqid)) {
			SharedPreferenceModule.getInstance().setString(USER_SEQID,
					userInfo.seqid);
			info.seqid = userInfo.seqid;
		}
		if (!TextUtils.isEmpty(userInfo.nickname)) {
			SharedPreferenceModule.getInstance().setString(USER_NICKNAME,
					userInfo.nickname);
			info.nickname = userInfo.nickname;
		}
		if (!TextUtils.isEmpty(userInfo.gender)) {
			SharedPreferenceModule.getInstance().setString(USER_GENDER,
					userInfo.gender);
			info.gender = userInfo.gender;
		}
		if (!TextUtils.isEmpty(userInfo.facepicurl)) {
			SharedPreferenceModule.getInstance().setString(
					USER_HEAd_PIC,
					FoneUtil.getAbsoluteUrl(info.host, info.shost,
							info.facepicurl));
			info.facepicurl = userInfo.facepicurl;
		}
		if (!TextUtils.isEmpty(userInfo.displayid)) {
			SharedPreferenceModule.getInstance().setString(USER_DISPLAY_ID,
					userInfo.displayid);
			info.displayid = userInfo.displayid;
		}
		if (!TextUtils.isEmpty(userInfo.userid)) {
			SharedPreferenceModule.getInstance().setString(USER_ID,
					userInfo.userid);
			info.userid = userInfo.userid;
		}
		if (!TextUtils.isEmpty(userInfo.feeurl)) {
			SharedPreferenceModule.getInstance().setString(USER_FEEURL,
					userInfo.feeurl);
			info.feeurl = userInfo.feeurl;
		}
		if (!TextUtils.isEmpty(userInfo.vipexpirestime)) {
			SharedPreferenceModule.getInstance().setString(
					USER_VIP_EXPIRESTIME,
					FoneUtil.FormatedTime2String(userInfo.vipexpirestime,
							"yyyyMMdd"));
			info.vipexpirestime = userInfo.vipexpirestime;
		}
		if (userInfo.binds != null) {
			info.binds = userInfo.binds;
		}
		saveLoginState(true);
		// 序列化存储用户信息
		serializeUserInfo(info);
	}

	/**
	 * 保存新登录用户的用户信息
	 * 
	 * @param userInfo
	 */
	private static void saveNewUser(LoginspaceRst userInfo) {

		if (!TextUtils.isEmpty(userInfo.type)) {
			SharedPreferenceModule.getInstance().setString(USER_TYPE,
					userInfo.type);
		}
		if (!TextUtils.isEmpty(userInfo.viptype)) {
			SharedPreferenceModule.getInstance().setString(USER_VIP_STATE,
					userInfo.viptype);
		}
		if (!TextUtils.isEmpty(userInfo.isflag)) {
			if ("0".equals(userInfo.isflag)) {
				SharedPreferenceModule.getInstance().setBoolean(
						USER_MODIFY_ACCOUNT, false);
			} else {
				SharedPreferenceModule.getInstance().setBoolean(
						USER_MODIFY_ACCOUNT, true);
			}
		}
		if (!TextUtils.isEmpty(userInfo.seqid)) {
			SharedPreferenceModule.getInstance().setString(USER_SEQID,
					userInfo.seqid);
		}
		if (!TextUtils.isEmpty(userInfo.nickname)) {
			SharedPreferenceModule.getInstance().setString(USER_NICKNAME,
					userInfo.nickname);
		}
		if (!TextUtils.isEmpty(userInfo.gender)) {
			SharedPreferenceModule.getInstance().setString(USER_GENDER,
					userInfo.gender);
		}
		if (!TextUtils.isEmpty(userInfo.facepicurl)) {
			SharedPreferenceModule.getInstance().setString(
					USER_HEAd_PIC,
					FoneUtil.getAbsoluteUrl(userInfo.host, userInfo.shost,
							userInfo.facepicurl));
		}
		if (!TextUtils.isEmpty(userInfo.displayid)) {
			SharedPreferenceModule.getInstance().setString(USER_DISPLAY_ID,
					userInfo.displayid);
		}
		if (!TextUtils.isEmpty(userInfo.userid)) {
			SharedPreferenceModule.getInstance().setString(USER_ID,
					userInfo.userid);
		}
		if (!TextUtils.isEmpty(userInfo.feeurl)) {
			SharedPreferenceModule.getInstance().setString(USER_FEEURL,
					userInfo.feeurl);
		}
		if (!TextUtils.isEmpty(userInfo.vipexpirestime)) {
			SharedPreferenceModule.getInstance().setString(
					USER_VIP_EXPIRESTIME,
					FoneUtil.FormatedTime2String(userInfo.vipexpirestime,
							"yyyyMMdd"));
		}
		if (!TextUtils.isEmpty(userInfo.vb)) {
			SharedPreferenceModule.getInstance().setString(USER_VB_COUNT,
					userInfo.vb);
		}
		saveLoginState(true);
		// 序列化用户信息
		serializeUserInfo(userInfo);

	}

	/**
	 * 获取已登录用户的用户信息
	 * 
	 * @return
	 */
	public static LoginspaceRst getUserInfo() {
		return readserializedUserInfo();
	}

	/**
	 * 保存用户登录状态
	 * 
	 * @param isLogin
	 */
	public static void saveLoginState(boolean isLogin) {
		SharedPreferenceModule.getInstance().setBoolean(USER_LOGIN_STATE,
				isLogin);
	}

	/**
	 * 判断是否已登录
	 * 
	 * @return
	 */
	public static boolean isLogin() {
		return SharedPreferenceModule.getInstance()
				.getBoolean(USER_LOGIN_STATE);
	}

	/**
	 * 存储用户的用户类型
	 * 
	 * @return 用户类型
	 */
	public static void saveUserType(String type) {
		SharedPreferenceModule.getInstance().setString(USER_TYPE, type);
		LoginspaceRst info = readserializedUserInfo();
		if (info != null) {
			info.type = type;
			serializeUserInfo(info);
		}
	}

	/**
	 * 获取登录用户的用户类型
	 * 
	 * @return 用户类型
	 */
	public static String getUserType() {
		return SharedPreferenceModule.getInstance().getString(USER_TYPE);
	}

	/**
	 * 保存本地登录用户的vip状态
	 * 
	 * @param mContext
	 * @param vipType
	 *            vip状态
	 */
	public static void saveVipType(String vipType) {
		SharedPreferenceModule.getInstance().setString(USER_VIP_STATE, vipType);
		LoginspaceRst info = readserializedUserInfo();
		if (info != null) {
			info.viptype = vipType;
			serializeUserInfo(info);
		}
	}

	/**
	 * 获取已登录用户的vip状态
	 * 
	 * @param mContext
	 * @return vip状态
	 */
	public static String getVipType() {
		return SharedPreferenceModule.getInstance().getString(USER_VIP_STATE);
	}

	/**
	 * 判断登录用户是否为vip
	 * 
	 * @return
	 */
	public static boolean isVip() {
		String vipState = getVipType();
		if (TextUtils.isEmpty(vipState)
				|| ((!"1".equals(vipState)) && (!"2".equals(vipState)))) {
			return false;
		}
		return true;
	}

	/**
	 * 设置已经修改过帐号状态
	 */
	public static void saveIsModified(String isflag) {
		if ("0".equals(isflag)) {
			SharedPreferenceModule.getInstance().setBoolean(
					USER_MODIFY_ACCOUNT, false);
		} else {
			SharedPreferenceModule.getInstance().setBoolean(
					USER_MODIFY_ACCOUNT, true);
		}
		LoginspaceRst info = readserializedUserInfo();
		if (info != null) {
			info.isflag = isflag;
			serializeUserInfo(info);
		}
	}

	/**
	 * 判断是否已经修改过帐号
	 * 
	 * @return
	 */
	public static boolean isModified() {
		return SharedPreferenceModule.getInstance().getBoolean(
				USER_MODIFY_ACCOUNT);
	}

	/**
	 * 保存已登录帐号的Sqid
	 * 
	 * @param userId
	 */
	public static void saveUserSqid(String Sqid) {
		SharedPreferenceModule.getInstance().setString(USER_SEQID, Sqid);
		LoginspaceRst info = readserializedUserInfo();
		if (info != null) {
			info.seqid = Sqid;
			serializeUserInfo(info);
		}
	}

	/**
	 * 获取已登录用户的Sqid
	 * 
	 * @return
	 */
	public static String getUserSqid() {
		return SharedPreferenceModule.getInstance().getString(USER_SEQID);
	}

	/**
	 * 保存用户昵称
	 * 
	 * @param nickName
	 */
	public static void saveUserNickName(String nickName) {
		SharedPreferenceModule.getInstance().setString(USER_NICKNAME, nickName);
		LoginspaceRst info = readserializedUserInfo();
		if (info != null) {
			info.nickname = nickName;
			serializeUserInfo(info);
		}
	}

	/**
	 * 获取已登录用户的昵称
	 * 
	 * @return
	 */
	public static String getUserNickName() {
		return SharedPreferenceModule.getInstance().getString(USER_NICKNAME);
	}

	/**
	 * 保存用户性别
	 * 
	 * @param nickName
	 */
	public static void saveUserGender(String gender) {
		L.v(TAG, "saveUserGender", gender + "");
		SharedPreferenceModule.getInstance().setString(USER_GENDER, gender);
		LoginspaceRst info = readserializedUserInfo();
		if (info != null) {
			info.gender = gender;
			serializeUserInfo(info);
		}
	}

	/**
	 * 获取已登录用户的昵称
	 * 
	 * @return
	 */
	public static String getUserGender() {
		return SharedPreferenceModule.getInstance().getString(USER_GENDER);
	}

	/**
	 * 保存用户头像url
	 * 
	 * @param nickName
	 */
	public static void saveUserHeadPic(String url) {
		LoginspaceRst info = readserializedUserInfo();
		info.facepicurl = url;
		SharedPreferenceModule.getInstance().setString(USER_HEAd_PIC,
				FoneUtil.getAbsoluteUrl(info.host, info.shost, url));
		serializeUserInfo(info);
	}

	/**
	 * 获取已登录用户头像url
	 * 
	 * @return
	 */
	public static String getUserHeadPic() {
		return SharedPreferenceModule.getInstance().getString(USER_HEAd_PIC);
	}

	/**
	 * 保存用户displayid
	 * 
	 * @param userId
	 */
	public static void saveUserDisplayId(String displayId) {
		SharedPreferenceModule.getInstance().setString(USER_DISPLAY_ID,
				displayId);
		LoginspaceRst info = readserializedUserInfo();
		if (info != null) {
			info.displayid = displayId;
			serializeUserInfo(info);
		}
	}

	/**
	 * 获取已登录的用户displayid
	 * 
	 * @return
	 */
	public static String getUserDisplayId() {
		return SharedPreferenceModule.getInstance().getString(USER_DISPLAY_ID);
	}

	/**
	 * 保存用户帐号
	 * 
	 * @param userId
	 */
	public static void saveUserid(String userId) {
		SharedPreferenceModule.getInstance().setString(USER_ID, userId);
		LoginspaceRst info = readserializedUserInfo();
		if (info != null) {
			info.userid = userId;
			serializeUserInfo(info);
		}
	}

	/**
	 * 获取已登录的用户帐号
	 * 
	 * @return
	 */
	public static String getUserid() {
		return SharedPreferenceModule.getInstance().getString(USER_ID);
	}

	/**
	 * 保存已登录用户的vb数量
	 * 
	 * @param vbCount
	 */
	public static void saveUserVbCount(String vbCount) {
		SharedPreferenceModule.getInstance().setString(USER_VB_COUNT, vbCount);
		LoginspaceRst info = readserializedUserInfo();
		if (info != null) {
			info.vb = vbCount;
			serializeUserInfo(info);
		}
	}

	/**
	 * 获取用户剩余vb数量
	 * 
	 * @return
	 */
	public static String getUserVbCount() {
		String vbCount = SharedPreferenceModule.getInstance().getString(
				USER_VB_COUNT);
		return "".equals(vbCount) ? "0" : vbCount;
	}

	/**
	 * 保存已登录用户的计费url
	 * 
	 * @param vbCount
	 */
	public static void saveUserFeeUrl(String feeUrl) {
		SharedPreferenceModule.getInstance().setString(USER_FEEURL, feeUrl);
		LoginspaceRst info = readserializedUserInfo();
		if (info != null) {
			info.feeurl = feeUrl;
			serializeUserInfo(info);
		}
	}

	/**
	 * 获取用户计费url
	 * 
	 * @return
	 */
	public static String getUserFeeUrl() {
		return SharedPreferenceModule.getInstance().getString(USER_FEEURL);
	}

	/**
	 * 保存已登录用户的VIP到期时间 USER_VIP_EXPIRESTIME
	 * 
	 * @param vbCount
	 */
	public static void saveUserFeeExpirestime(String vipexpirestime,
			String inFromate) {

		if (TextUtils.isEmpty(inFromate)) {
			inFromate = "yyyyMMdd";
		}
		if (!TextUtils.isEmpty(vipexpirestime)) {
			SharedPreferenceModule.getInstance().setString(
					USER_VIP_EXPIRESTIME,
					FoneUtil.FormatedTime2String(vipexpirestime, inFromate));
		} else {
			SharedPreferenceModule.getInstance().setString(
					USER_VIP_EXPIRESTIME, "");
		}
		LoginspaceRst info = readserializedUserInfo();
		if (info != null) {
			info.vipexpirestime = vipexpirestime;
			serializeUserInfo(info);
		}
	}

	/**
	 * 获取用户VIP到期时间
	 * 
	 * @return
	 */
	public static String getUserFeeExpirestime() {
		return SharedPreferenceModule.getInstance().getString(
				USER_VIP_EXPIRESTIME);
	}

	/**
	 * 获取指定类型的绑定的第三方用户信息
	 * 
	 * @param SNSType
	 *            第三方用户平台
	 * @return SNSUserInfo
	 */
	public static SNSUserInfo getBindSNSUserInfo(int SNSType) {
		SNSUserInfo snsUserInfo = null;
		String temp = null;
		switch (SNSType) {
		case ISNSLoginManager.TENCENT_QQ_TYPE:
			temp = "qq";
			break;
		case ISNSLoginManager.TENCENT_WEIBO_TYPE:
			temp = "tencent";
			break;
		case ISNSLoginManager.SINA_WEIBO_TYPE:
			temp = "sina";
			break;
		}
		L.v(TAG, "getBindSNSUserInfo", "SNSType : " + temp);
		if (TextUtils.isEmpty(temp)) {
			return null;
		}

		LoginspaceRst localUserInfo = readserializedUserInfo();
		if (localUserInfo == null || localUserInfo.binds == null
				|| localUserInfo.binds.bindList == null
				|| localUserInfo.binds.bindList.size() == 0) {
			return null;
		}

		for (Bind bind : localUserInfo.binds.bindList) {
			if (temp.equals(bind.fp)) {
				snsUserInfo = new SNSUserInfo();
				snsUserInfo.snsType = SNSType;
				snsUserInfo.accessToken = bind.access_token;
				snsUserInfo.expiresIn = bind.expiresIn;
				snsUserInfo.expiresTime = bind.expirestime;
				snsUserInfo.thirdPlatformID = bind.sid;
				L.v(TAG, "getBindSNSUserInfo", "snsUserInfo : " + snsUserInfo);
				return snsUserInfo;
			}
		}
		return null;
	}

	/**
	 * 获取已经绑定的所有第三方授权
	 * 
	 * @return
	 */
	public static List<SNSUserInfo> getBinds() {
		ArrayList<SNSUserInfo> binds = null;
		LoginspaceRst localUserInfo = readserializedUserInfo();
		if (localUserInfo == null || localUserInfo.binds == null
				|| localUserInfo.binds.bindList == null
				|| localUserInfo.binds.bindList.size() == 0) {
			return binds;
		}
		binds = new ArrayList<SNSUserInfo>();
		for (Bind bind : localUserInfo.binds.bindList) {
			SNSUserInfo snsUserInfo = new SNSUserInfo();
			if ("qq".equals(bind.fp)) {
				snsUserInfo.snsType = ISNSLoginManager.TENCENT_QQ_TYPE;
			} else if ("tencent".equals(bind.fp)) {
				snsUserInfo.snsType = ISNSLoginManager.TENCENT_WEIBO_TYPE;
			} else if ("sina".equals(bind.fp)) {
				snsUserInfo.snsType = ISNSLoginManager.SINA_WEIBO_TYPE;
			} else {
				continue;
			}
			snsUserInfo.accessToken = bind.access_token;
			snsUserInfo.expiresIn = bind.expiresIn;
			snsUserInfo.expiresTime = bind.expirestime;
			snsUserInfo.thirdPlatformID = bind.sid;
			binds.add(snsUserInfo);
		}
		L.v(TAG, "getBinds", "binds : " + binds);
		return binds;
	}

	/**
	 * 更新绑定
	 * 
	 * @param info
	 */
	public static void upBinds(SNSUserInfo info) {
		String temp = null;
		switch (info.snsType) {
		case ISNSLoginManager.TENCENT_QQ_TYPE:
			temp = "qq";
			break;
		case ISNSLoginManager.TENCENT_WEIBO_TYPE:
			temp = "tencent";
			break;
		case ISNSLoginManager.SINA_WEIBO_TYPE:
			temp = "sina";
			break;
		}
		L.v(TAG, "getBindSNSUserInfo", "SNSType : " + temp);
		if (TextUtils.isEmpty(temp)) {
			return;
		}
		LoginspaceRst localUserInfo = readserializedUserInfo();
		if (localUserInfo == null) {
			return;
		}
		if (localUserInfo.binds == null) {
			localUserInfo.binds = new Binds();
		}
		if (localUserInfo.binds.bindList == null) {
			localUserInfo.binds.bindList = new ArrayList<LoginspaceRst.Bind>();
		}
		Bind bind = new Bind();
		bind.fp = temp;
		bind.sid = info.thirdPlatformID;
		bind.access_token = info.accessToken;
		bind.expiresIn = info.expiresIn;
		bind.expirestime = info.expiresTime;
		for (int i = 0; i < localUserInfo.binds.bindList.size(); i++) {
			if (localUserInfo.binds.bindList.get(i).equals(bind.fp)) {
				localUserInfo.binds.bindList.remove(i);
				break;
			}
		}
		localUserInfo.binds.bindList.add(bind);
		serializeUserInfo(localUserInfo);
	}

	/**
	 * 根据Type删除绑定
	 * 
	 * @param SNSType
	 */
	public static void removeBind(int SNSType) {
		String temp = null;
		switch (SNSType) {
		case ISNSLoginManager.TENCENT_QQ_TYPE:
			temp = "qq";
			break;
		case ISNSLoginManager.TENCENT_WEIBO_TYPE:
			temp = "tencent";
			break;
		case ISNSLoginManager.SINA_WEIBO_TYPE:
			temp = "sina";
			break;
		}
		L.v(TAG, "getBindSNSUserInfo", "SNSType : " + temp);
		if (TextUtils.isEmpty(temp)) {
			return;
		}
		LoginspaceRst localUserInfo = readserializedUserInfo();
		if (localUserInfo == null || localUserInfo.binds == null
				|| localUserInfo.binds.bindList == null
				|| localUserInfo.binds.bindList.size() == 0) {
			return;
		}

		for (int i = 0; i < localUserInfo.binds.bindList.size(); i++) {
			if (localUserInfo.binds.bindList.get(i).fp.equals(temp)) {
				localUserInfo.binds.bindList.remove(i);
				break;
			}
		}
		serializeUserInfo(localUserInfo);
		SNSLoginManager.getInstance().CancelAuthSNS(SNSType);// 移除授权
	}

	/**
	 * 保存用户信息到本地
	 * 
	 * @param userInfo
	 */
	private static void serializeUserInfo(LoginspaceRst userInfo) {
		// TODO LoginspaceRst 转换为 字符串
		L.v(TAG, "serializeUserInfo", " serializeUserInfo  start !!!");
		RstSerializer rstSerializer = new RstSerializer();
		String infoString = rstSerializer.toString(userInfo);
		if (infoString == null) {
			L.e(TAG, "serializeUserInfo", " RstSerializer  toString  error !!!");
			return;
		}
		FileOutputStream fos = null;
		try {
			fos = mContext.openFileOutput(USER, Context.MODE_PRIVATE);
			fos.write(infoString.getBytes());
			fos.flush();
			L.v(TAG, "serializeUserInfo", " serializeUserInfo  end !!!");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					L.e(TAG, "serializeUserInfo", e.getMessage());
				}
			}
		}
	}

	/**
	 * 读取本地用户信息文件
	 * 
	 * @return
	 */
	private static LoginspaceRst readserializedUserInfo() {
		L.v(TAG, "serializeUserInfo", " readserializedUserInfo  start !!!");
		LoginspaceRst userInfo = null;
		File data = mContext.getFileStreamPath(USER);
		if (!data.exists()) {
			return null;
		}
		FileInputStream fis = null;
		ByteArrayOutputStream bos = null;
		try {
			fis = mContext.openFileInput(USER);
			bos = new ByteArrayOutputStream();

			byte[] bytes = new byte[1024];
			int bytesLong = 0;

			while ((bytesLong = fis.read(bytes)) != -1) {
				bos.write(bytes, 0, bytesLong);
			}
			String rst = bos.toString();
			// TODO 转化为 LoginspaceRst
			RstSerializer rstSerializer = new RstSerializer();
			userInfo = rstSerializer.fromString(LoginspaceRst.class, rst);
			L.v(TAG, "serializeUserInfo", " readserializedUserInfo  end !!!");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
			L.e(TAG, "LoginspaceRst", " RstSerializer  fromString error ");
		} finally {
			try {
				if (fis != null) {
					fis.close();
				}
				if (bos != null) {
					bos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return userInfo;
	}

	/**
	 * 移除本地序列化用户信息
	 */
	private static void removeLocalUserInfo() {
		File data = mContext.getFileStreamPath(USER);
		if (data.exists()) {
			data.delete();
		}
	}

	private static void clearUserInfo() {
		SharedPreferenceModule.getInstance().setString(USER_TYPE, "");
		SharedPreferenceModule.getInstance().setString(USER_VIP_STATE, "");
		SharedPreferenceModule.getInstance().setBoolean(USER_MODIFY_ACCOUNT,
				true);
		SharedPreferenceModule.getInstance().setString(USER_SEQID, "");
		SharedPreferenceModule.getInstance().setString(USER_NICKNAME, "");
		SharedPreferenceModule.getInstance().setString(USER_GENDER, "");
		SharedPreferenceModule.getInstance().setString(USER_HEAd_PIC, "");
		SharedPreferenceModule.getInstance().setString(USER_DISPLAY_ID, "");
		SharedPreferenceModule.getInstance().setString(USER_ID, "");
		SharedPreferenceModule.getInstance().setString(USER_FEEURL, "");
		SharedPreferenceModule.getInstance()
				.setString(USER_VIP_EXPIRESTIME, "");
	}

	/**
	 * 退出登录
	 */
	public static void exitLogin() {
		L.v(TAG, "exitLogin", " exitLogin start !!!");
		saveLoginState(false);
		updateCipher("");
		clearUserInfo();
		removeLocalUserInfo();
		deleteData();
		FoneConstant.isLogin = false;
		// SNSLoginManager loginManager = SNSLoginManager.getInstance();
		// loginManager.CancelAllAuthSNS();
		// 清空收藏数据库
		//
	}

	private static class QuickPlayCallBack implements Callback<CollectionlsRst> {

		@Override
		public void onSuccess(CollectionlsRst t) {
			L.i(TAG, "同步QuickPlayCallBack  success!");
			if (t.result == 0 && t.cnts != null) {
				List<Cnt> cntList = t.cnts.cntList;
				if (cntList == null || cntList.size() == 0) {
					L.i(TAG, "同步QuickPlayCallBack:  cntList is null!");
					return;
				} else {
					int result = StorageModule.getInstance()
							.deletePlayRecordList(
									PlayRecord.RECORD_PLAYER_NETWORK);
					if (result == 1) {
						List<PlayRecord> list = new ArrayList<PlayRecord>();
						fillList(list, cntList, t.host, t.shost);
						int result2 = StorageModule.getInstance()
								.addPlayRecordList(list);
						L.i(TAG, "同步QuickPlayCallBack: result2:" + result2);
					}
				}
			}
		}

		@Override
		public void onFailure(Error error) {

			L.i(TAG, "同步QuickPlayCallBack   error.state:" + error.getStatus());
			L.i(TAG, "同步QuickPlayCallBack   error.reason:" + error.getReason());

		}
	}

	// 将cntList中的对象转换为PlayRecord放入到list中
	private static void fillList(List<PlayRecord> list, List<Cnt> cntList,
			String host, String shost) {
		for (Cnt cnt : cntList) {
			cnt.pic = FoneUtil.getAbsoluteUrl(host, shost, cnt.pic);
			PlayRecord record = new PlayRecord();
			record.setPlayRecordDetailUrl(cnt.durl);
			record.setPlayRecordType(PlayRecord.RECORD_PLAYER_NETWORK);
			record.setPlayRecordName(cnt.name);
			record.setPlayRecordImageUrl(cnt.pic);
			record.setPlayRecordRemoveUrl(cnt.rurl);
			record.setPlayRecordTotalTime(cnt.mins);
			record.setPlayRecordAlreadyPlayTime(cnt.tp);
			record.setPlayRecordContentType(cnt.type);
			record.setPlayRecordVideoID(cnt.videoid);
			record.setPlayRecordCreateTime(cnt.udate);
			record.setPlayRecordEpisodeUpdateInfo(cnt.updateinfo);
			record.setPlayRecordSynchronzieType(PlayRecord.PLAY_RECORD_SYNCHRONZIE_ADD_ALREADY);
			cnt.weibourl = FoneUtil.getAbsoluteUrl(host, shost, cnt.weibourl);
			record.setPlayRecordCCID(FoneUtil.String2Long(FoneUtil
					.getCCIdByUrl(cnt.url)));
			record.setPlayRecordCID(FoneUtil.String2Long(FoneUtil
					.getCIdByUrl(cnt.url)));
			record.setPlayRecordCLID(FoneUtil.String2Long(FoneUtil
					.getCLIdByUrl(cnt.url)));
			record.setPlayRecordShareUrl(cnt.weibourl);
			record.setPlayRecordPlayUrl(cnt.url);
			record.setPlayRecordIsVIP(cnt.vip == 1);
			// record.setPlayRecordDescription(cnt.)
			// record.setPlayRecordFavouriteUrl(cnt.rurl);
			list.add(record);
		}
	}

	public static void saveHistoricalUserList(String userName) {

		L.v(TAG, "saveHistoricalUserList", "userName : " + userName);
		if (TextUtils.isEmpty(userName)) {
			return;
		}
		ArrayList<String> userList = SerializationUtil
				.readUserListSerialization(mContext, HISTORICALUSERLIST);
		if (userList == null) {
			userList = new ArrayList<String>();
		}
		if (userList.contains(userName)) {
			userList.remove(userName);
		}
		userList.add(0, userName);
		if (userList.size() >= 5) {
			for (int i = 0; i < userList.size(); i++) {
				if (i >= 5) {
					userList.remove(i);
				}
			}
		}
		SerializationUtil.wirteUserListSerialization(mContext, userList,
				HISTORICALUSERLIST);
	}

	/**
	 * 更新Cipher
	 * 
	 * @param seqid
	 */
	private static void updateCipher(String seqid) {
		Request.getInstance().updateCipher(seqid);
		Request.getNotifcationInstance(mContext).updateCipher(seqid);
		Request.getSearchInstance(mContext).updateCipher(seqid);
		Request.getInstance().updateFeeCipher(seqid);
	}

	/**
	 * 清空有关登陆用户的数据
	 */
	private static void deleteData() {
		SharedPreferences mSp = mContext.getSharedPreferences(
				FoneConstant.FONE_SP_NAME_SP, Context.MODE_PRIVATE);
		Editor edit = mSp.edit();
		edit.putInt(FoneConstant.COLLETION_TV_UPDATE_COUNT, 0);
		edit.commit();
		int deleteResult = StorageModule.getInstance().deleteFavouriteAll();
		L.v(TAG, "deleteData", "deleteResult=" + deleteResult);
		int deletePlayRecordResult = StorageModule.getInstance()
				.deletePlayRecordList(PlayRecord.RECORD_WEB_ALL);
		L.v(TAG, "deleteData", "deletePlayRecordResult="
				+ deletePlayRecordResult);
		FoneUtil.setRoundButtonImage();
	}

}
